<?php
function nodecode($jsdata)
{
    global $url, $gather_type;
    $str="";
    if($jsdata==null) return;
    //一、先用最简单的处理,看是否成功
    $result = json_decode($jsdata, 1);
    if (is_array($result)) {
        return json_decode(json_encode($result), 1);
    } else if (is_array($result)) {
        return $result;
    } else {
        $jsdata = cleanjsonstr($jsdata);
        $result = json_decode($jsdata, 1);
        if (is_array($result)) {
            return $result;
        }else{
            $jsdata = cleanjsonstr2($jsdata);
            $result = json_decode($jsdata, 1);
            if (is_array($result)) {
                return $result;
            }
        }

        $data = json_decode_my($jsdata);
        if (is_array($data)) {
            return $data;
        } else {
            //三、自己用程序一节节分析生成数组,看是否成功
            $nni = 0;
            $NNi = 0;
            $ssi = 0;
            $nnnn = array();
            $NNNN = array();
            $str2 = array();
            $cheng = 1;

            preg_match_all("/((?<!\\\)\"[a-zA-Z0-9_]*(?<!\\\)\")(?<=\"):\s*(?<!\\\)\"([^\{\}\[\]]*)(?<!\\\)\"/Uisx", $str, $nn);
            for ($i = 0; $i < count($nn[1]); $i++) {
                $str = str_replace($nn[0][$i], $nn[1][$i] . ":\"\$strstr{$ssi}\"", $str);
                $nn[2][$i] = @mb_convert_encoding($nn[2][$i], "utf-8", "utf-8,GB2312, GBK,ISO8859-1");
                ${"strstr" . $ssi} = $nn[2][$i];
                $str2[$ssi] = ${"strstr" . $ssi};
                $ssi++;
            }

            do {
                //这里处理有:的对象型
                do {
                    $pattern = "/(?:(\{)|\[)((?:\"[a-zA-Z0-9-_\/\.\?&=\\\#]+\"\s*:\s*(?:\"[^\[\{\}\]]+\"|\\\$nnnn[0-9]+|\[\]|null|true|false|[0-9]+)\,?)+)(?(1)\}|\])/Uisx";
                    try {
                        preg_match_all($pattern, $str, $nn);
                    } catch (Exception $e) {
                        last($url, $gather_type, "yzs1013_pldr.php 表达式出错1：$pattern \r\n\r\nstr:\n$str");
                    }
                    for ($i = 0; $i < count($nn[0]); $i++) {
                        $str = str_replace($nn[0][$i], "\$nnnn{$nni}", $str);
                        $nn[0][$i] = preg_replace("/\\\t/", "tttttt", $nn[0][$i]);
                        $nn[0][$i] = preg_replace_callback("/\\\$(nnnn)([0-9]+)/i", function ($em) use ($NNNN, $nnnn) {
                            return json_encode(${$em[1]}[$em[2]]);
                        }, $nn[0][$i]);
                        $nn[0][$i] = @mb_convert_encoding($nn[0][$i], "utf-8", "utf-8,GB2312, GBK,ISO8859-1");
                        ${"nnnn" . $nni} = json_decode($nn[0][$i], 1);
                        $nnnn[$nni] = ${"nnnn" . $nni};
                        $nni++;
                    }
                } while (preg_match($pattern, $str));

                //这个处理没有:号的数组型
                do {
                    $pattern2 = "/(?:(\{)|\[)((?:\"[^\[\]\{\}]+\"|\\\$nnnn[0-9]+|\[\]|true|false|null)\,?)+(?(1)\}|\])/Uisx";
                    try {
                        preg_match_all($pattern2, $str, $nn2);
                    } catch (Exception $e) {
                        last($url, $gather_type, "yzs1013_pldr.php 表达式出错2：$pattern2 \r\n\r\nstr:\n$str");
                    }
                    for ($i = 0; $i < count($nn2[0]); $i++) {
                        $str = str_replace($nn2[0][$i], "\$NNNN{$NNi}", $str);
                        ${"NNNN" . $NNi} = strToArr($nn2[0][$i], $nnnn, $NNNN);
                        $NNNN[$NNi] = ${"NNNN" . $NNi};
                        $NNi++;
                    }
                } while (preg_match($pattern2, $str));
                $cheng++;
                //echo $str . "\r\n\r\n\r\n";
            } while (preg_match($pattern, $str) || preg_match($pattern2, $str));
            preg_match("/\\\$(nnnn)([0-9]+)/i", $str, $nn);
            $result = eval("return " . $str . ";");
            $json = json_encode($result);
            $json = preg_replace_callback("/.strstr([0-9]+)/isx", function ($em) use ($str2) {
                return preg_replace("/(?<!\\\)\"/Uisx", "\\\"", $str2[$em[1]]);
            }, $json);
            $result = json_decode($json, 1);
            if (is_array($result)) {
                return $result;
            }
        }
        //四、最后用提交给nodejs处理,看是否成功
        if($jsdata==null||$jsdata==0) return;
        $data['jsdata'] = $jsdata;
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, 'http://120.77.245.205/python/jsdata.php?XDEBUG_SESSION_START=20181012144922');
        //curl_setopt($curl, CURLOPT_URL, 'http://word.longsunhd.com/python/jsdata.php?XDEBUG_SESSION_START=20181012144922');
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0");
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Host:' .'120.77.245.205',
            'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0',
            'Connection:  keep-alive',
            'Accept-Language: en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7',
            'Accept: application/json, text/javascript, */*; q=0.01',
            'X-Requested-With: XMLHttpRequest',
        ));
        $data = curl_exec($curl);
        curl_close($curl);
        $result=json_decode($data, 1);
        //print_r($result);exit;
        return $result;
    }
}



function json_decode_my($tempnr)
{
    $data = h1c($tempnr);
    $data = json_decode_my_inc($data);
    return $data;
}

function json_decode_my_inc($data)
{
    if(!empty($data)) {
        foreach ($data as $k => $v) {
            if (is_string($v) && preg_match("/strostr([0-9]+)/isx", $v, $nn)) {
                $data[$k] = stripcslashes(unicode_to_utf8($GLOBALS['strostr'][$nn[1]]));
            } elseif (is_string($v)) {
                $data[$k] = $v;
            } elseif (is_array($v)) {
                $data[$k] = json_decode_my_inc($v);
            }
        }
    }
    return $data;
}

function unicode_to_utf8($str)
{
    $str = preg_replace_callback("#\\\u([0-9a-f]+)#i", function ($m) {
        return iconv('UCS-2', 'UTF-8', pack('H4', $m[1]));
    }, $str);
    return $str;
}


function h1c($tempnr)
{
    static $jsonindex = 0;
    $tempnr = preg_replace_callback("/\:\"(.*)(?<!\\\)\"/Uisx", function ($em) use ($jsonindex) {
        static $jsonindex;
        $jsonindex++;
        $GLOBALS['strostr'][$jsonindex] = $em[1];
        return ":\"strostr{$jsonindex}\"";
    }, $tempnr);
    $tempnr = preg_replace_callback("/\,([\}\]])/Ui", function ($em) {
        return $em[1];
    }, $tempnr);
    return json_decode($tempnr, 1);
}

function cleanjsonstr($jsdata)
{
    //二、用正则将字串修改成标准的,看是否成功
    //把回车去掉，json不需要回车
    $str = preg_replace("/\s*(\r|\n)\s*/", "", $jsdata);
    $str = preg_replace_callback("/^[\{\[]\\\\\".*$/Uisx", function ($em2) {
        $em2[0]=preg_replace("/(\\\u[0-9A-Za-z]{4})/","\\\\\\1",$em2[0]);
        $em2[0] = stripcslashes($em2[0]);
        return $em2[0];
    }, $str);
    $str = str_replace("&quot;", "MAOHAO", $str);
    $str = str_replace(":undefined,", ":0,", $str);
    $str = preg_replace("/\\\u003C/Uisx", "<", $str);
    $str = preg_replace("/\\\u003e/Uisx", ">", $str);
    $str = preg_replace("/\\\u002F/Uisx", "/", $str);
    //$str = preg_replace("/'/Uisx", "\u2018", $str);
    $str = preg_replace("/\(/Uisx", "\uFF08", $str);
    $str = preg_replace("/\)/Uisx", "\uFF09", $str);
    $str = str_replace('&#x3D;', "=", $str);
    $str=preg_replace_callback("/\\\\\\\"([^\"]+)\\\"/Uisx",function($em){
        if(preg_match("/((?:\\\)+)\\\u/isx",$em[0],$nn22)) {
            for($i=0;$i<strlen($nn22[1]);$i++) {
                $em[1] = stripcslashes($em[1]);
            }
            $em[1] = unicode_to_utf8($em[1]);
        }
        if(preg_match("/((?:\\\)+)\//isx",$em[0],$nn22)) {
            for($i=0;$i<strlen($nn22[1]);$i++) {
                $em[1] = stripcslashes($em[1]);
            }
        }
        return "\\\"{$em[1] }\"";
    },$str);
    $str = str_replace("/‘/Uisx", "", $str);


    //把冒号前面的没有双引号的键值，用引号框住
    $str = preg_replace_callback("/(?<=(?<=\[|\:|\{|^|\s)\{|\,|(?<=\[|\:|\{)\[)\s*([a-zA-Z0-9_]+)\s*\:\s*(?=\\\\\"|\"|\'|\\\$nnnn[0-9]+|\{|\[|\[\]|null|true|false|[0-9]+)/Uisx", function ($em) {
        return "\"{$em[1]}\":";
    }, $str);

    $str = preg_replace_callback("/(?<=\:)\s*'(.*)'\s*(?=\,|\.|\}|\])/Uisx", function ($em) {
        $em[1] = str_replace('\\', "\\\\", $em[1]);
        $em[1] = str_replace('"', "\\\"", $em[1]);
        $em[1] = preg_replace_callback("/^\\\\\"(.*)\\\\\"$/i", function ($em2) {
            $em2[1] = stripcslashes($em2[1]);
            return $em2[1];
        }, $em[1]);
        return "\"" . $em[1] . "\"";
    }, $str);

    //把大括号和中括号内的单引号字符串，替换成双引号字串
    $str = preg_replace_callback("/(?<=\{|,|\[|\:)\s*(?<!\\\)'(.*)(?<!\\\)'?\s*(?=\,\s*\"|\]|\})/Uisx", function ($em) {
        $em[1] = str_replace('\\', "\\\\", $em[1]);
        $em[1] = str_replace('"', "\\\"", $em[1]);
        return "\"" . $em[1] . "\"";
    }, $str);

    //去掉剩下的单引号前面的线,以免造成冲突
    $str = preg_replace("/\\\'/Ui", "'", $str);

    //去掉字符后面的.函数处理串
    $str = preg_replace("/\"\.(.*)\,\"(?=[a-zA-Z0-9_]+\"\s*\:)/Uisx", "\",\"", $str);

    //去掉变身的引号,以免造成冲突
    $str = preg_replace_callback("/(?<!\\\)\"(.*)(?<!\\\)\"/Ui", function ($em) {
        return "\"{$em[1]}\"";
    }, $str);



    //去掉元素项，逗号尾巴
    $str = preg_replace_callback("/\,\s*([\}\]])/Ui", function ($em) {
        return $em[1];
    }, $str);
    $str = str_replace("MAOHAO", "\\\"", $str);
    return $str;
}

function cleanjsonstr2($jsdata)
{
    $jsdata=preg_replace("/(\,|\:|\[|\{)\s*\\\\\\\\\"/Uisx","\\1\"",$jsdata);
    $jsdata=preg_replace("/\\\\\\\\\"(?=\,|\}|\:|\])/Uisx","\"",$jsdata);
    $jsdata=preg_replace("/(\,|\:|\[|\{)\s*\\\\\"/Uisx","\\1\"",$jsdata);
    $jsdata=preg_replace("/(\,|\:|\[|\{)\s*\\\\\"/Uisx","\\1\"",$jsdata);
    $jsdata=preg_replace("/\\\\\"(?=\,|\:|\}|\])/Uisx","\"",$jsdata);
    $jsdata=preg_replace("/\\\\\"(?=\,|\:|\}|\])/Uisx","\"",$jsdata);
    $jsdata=preg_replace("/\"\{/Uisx","{",$jsdata);
    $jsdata=preg_replace("/\}\"/Uisx","}",$jsdata);
    $jsdata=preg_replace("/\"\[/Uisx","[",$jsdata);
    $jsdata=preg_replace("/\]\"/Uisx","]",$jsdata);
    $jsdata=preg_replace("/(\,|\:|\[|\{)\s*(\\\)+\\\"/Uisx","\\1\"",$jsdata);
    $jsdata=preg_replace("/(\\\)+\\\"(?=\,|\}|\:|\])/Uisx","\"",$jsdata);
    return $jsdata;
}